package org.eclipse.jdt.internal.ui.callhierarchy;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jdt.core.IField;

public class FieldLombokRefactorBuilder {
	
	private static FieldLombokRefactorBuilder singleton = new FieldLombokRefactorBuilder();
	
	private List<FieldLombokRefactor> refactors =  new ArrayList<FieldLombokRefactor>();
	
	private FieldLombokRefactorBuilder() {
		RefactorClassBuilder refactorClassBuilder = new DefaultRefactorClassBuilder();
		TemplateEngine templateEngine = new TemplateEngine();
		
		AbstractFieldRefactor normalGetterRefactor = new NormalGetterFieldRefactor();
		normalGetterRefactor.setRefactorClassBuilder(refactorClassBuilder);
		normalGetterRefactor.setTemplateEngine(templateEngine);
		register(normalGetterRefactor);
		
		AbstractFieldRefactor normalSetterRefactor = new NormalSetterFieldRefactor();
		normalSetterRefactor.setRefactorClassBuilder(refactorClassBuilder);
		normalSetterRefactor.setTemplateEngine(templateEngine);
		register(normalSetterRefactor);
		
		AbstractFieldRefactor autoloadGetterRefactor = new AutoLoadGetterFieldRefactor();
		autoloadGetterRefactor.setRefactorClassBuilder(refactorClassBuilder);
		autoloadGetterRefactor.setTemplateEngine(templateEngine);
		register(autoloadGetterRefactor);
		
		AbstractFieldRefactor callSuperSetterRefactor = new CallSuperSetterFieldRefactor();
		callSuperSetterRefactor.setRefactorClassBuilder(refactorClassBuilder);
		callSuperSetterRefactor.setTemplateEngine(templateEngine);
		register(callSuperSetterRefactor);
		
		AbstractFieldRefactor valueNameGetterRefactor = new ValueNameFieldRefactor();
		valueNameGetterRefactor.setRefactorClassBuilder(refactorClassBuilder);
		valueNameGetterRefactor.setTemplateEngine(templateEngine);
		register(valueNameGetterRefactor);
		
		AbstractFieldRefactor noneFieldRefactor = new NoneFieldRefactor();
		noneFieldRefactor.setRefactorClassBuilder(refactorClassBuilder);
		noneFieldRefactor.setTemplateEngine(templateEngine);
		register(noneFieldRefactor);
	}
	
	public void register(FieldLombokRefactor refactor) {
		refactors.add(refactor);
	}
	
	public static FieldLombokRefactorBuilder getInstance() {
		return singleton;
	}

	public FieldLombokRefactor buildFieldRefactor(IField field) {
		CompositeFieldRefactor compoiteRefactor = new CompositeFieldRefactor();
		
		if (refactors != null
				&& refactors.size() > 0) {
			for (FieldLombokRefactor refactor
					: refactors) {
				if (refactor.match(field)) {
					compoiteRefactor.add(refactor);
				}
			}
		}
		
		return compoiteRefactor;
	}
}
